package com.yoyo.ad.baidu;

import android.app.Activity;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;

import com.baidu.mobad.feeds.NativeResponse;
import com.baidu.mobad.nativevideo.BaiduVideoResponse;
import com.yoyo.ad.main.AdModelType;
import com.yoyo.ad.main.YoYoAd;
import com.yoyo.ad.utils.StringUtil;

import static com.yoyo.ad.confusion.AdManage.BAIDU;


class BaiduYoYoAd implements YoYoAd {

    private boolean isNative;

    BaiduYoYoAd(NativeResponse nativeResponse) {
        this.mNativeResponse = nativeResponse;
    }

    public BaiduYoYoAd(BaiduVideoResponse baiduVideoResponse) {
        mBaiduVideoResponse = baiduVideoResponse;
    }

    private NativeResponse mNativeResponse;
    private BaiduVideoResponse mBaiduVideoResponse;

    @Override
    public int getSource() {
        return BAIDU;
    }

    @Override
    public String getTitle() {
        if (mNativeResponse != null) {
            return mNativeResponse.getTitle();
        }
        return null;
    }

    @Override
    public String getDescription() {
        if (mNativeResponse != null) {
            return mNativeResponse.getDesc();
        }
        return null;
    }

    @Override
    public Bitmap getLogo() {
        return null;
    }

    @Override
    public String getIconUrl() {
        if (mNativeResponse != null) {
            return mNativeResponse.getIconUrl();
        }
        return null;
    }

    @Override
    public String getImgUrl1() {
        if (mNativeResponse != null) {
            return StringUtil.notNull(mNativeResponse.getImageUrl(),
                    (mNativeResponse.getMultiPicUrls() != null && mNativeResponse.getMultiPicUrls().size() > 0) ?
                            mNativeResponse.getMultiPicUrls().get(0) : null);
        }
        return null;
    }

    @Override
    public String getImgUrl2() {
        if (mNativeResponse != null) {
            return (mNativeResponse.getMultiPicUrls() != null && mNativeResponse.getMultiPicUrls().size() > 1) ?
                    mNativeResponse.getMultiPicUrls().get(1) : null;
        }
        return null;
    }

    @Override
    public String getImgUrl3() {
        if (mNativeResponse != null) {
            return (mNativeResponse.getMultiPicUrls() != null && mNativeResponse.getMultiPicUrls().size() > 2) ?
                    mNativeResponse.getMultiPicUrls().get(2) : null;
        }
        return null;
    }

    @Override
    public String getBrandName() {
        if (mNativeResponse != null) {
            return StringUtil.notNull(mNativeResponse.getBrandName(), "百度");
        }
        return null;
    }

    @Override
    public int getModel() {
        if (mNativeResponse != null) {
            if (mNativeResponse.getAdMaterialType().equals(NativeResponse.MaterialType.VIDEO.getValue())) {
                return AdModelType.MODEL_VIDEO;
            } else if (mNativeResponse.getAdMaterialType().equals(NativeResponse.MaterialType.NORMAL.getValue())) {
                return AdModelType.MODEL_IMAGE;
            }
        }
        return AdModelType.MODEL_UNKNOW;
    }

    @Override
    public View getView() {
        return null;
    }

    @Override
    public boolean isNativeExpress() {
        return isNative;
    }

    @Override
    public void exposure(View view) {

    }

    @Override
    public void onAdClicked(final View view, final ViewGroup viewGroup) {
        if (viewGroup != null)
            viewGroup.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mNativeResponse != null) {
                        mNativeResponse.handleClick(viewGroup);
                    }
                }
            });
        else
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mNativeResponse != null) {
                        mNativeResponse.handleClick(view);
                    }
                }
            });
    }

    @Override
    public void show(Activity activity) {

    }

    @Override
    public void release() {

    }
}
